/**
 *	Copyright (c) TeamFlamingo And gray 2004-2009
 *	www.me-live.com.cn / www.i-gray.cn
 *
 *	[ WARNING ]
 *		This is NOT a freeware!
 *		You MUST get author's authorization before using it!
 *	[ WARNING ]
 *
 *	Name : meEditorplus.js / Development Code Evolve
 *	Created / Modify : 2010-3-31 / --
 */
var	meEditorplus	=	Class
({
	Version	:	'2.0.1 Alpha',
	Author	:	'gray',

	//	[CH]	系统设置
	editPre	:	'meEditor_',
	imgDir	:	'./images/m/',
	smilieDir	:	'./images/smile/',
	iframeCssFiles	:	'./app/templates/default/css/iframe.css',

	size	:	[ 1, 2, 3, 4, 5, 6, 7 ],

	//	[CH]	定义工具架
	tools	:	[
		'bold', 'italic', 'underline', 'split',
		'justifyleft', 'justifycenter', 'justifyright', 'split',
		'outdent', 'indent', 'insertorderedlist', 'insertunorderedlist', 'split',
		'url', 'email', 'quote', 'code', 'hide', 'split',
		'flv', 'image', 'split',
		'forecolor', 'backcolor'
	],

	//	[CH]	定义可选的代码类型
	codeType	:	[
		'代码类型', 'JavaScript', 'HTML', 'PHP'
	],

	//	[CH]	字体列表
	family	:	[
		'宋体', '黑体', '隶书', '楷体', '楷体_GB2312',
		'幼圆', 'Arial', 'Impact', 'Georgia',
		'Verdana', 'Courier New', 'Times New Roman'
	],

	//	[CH]	颜色列表
	color	:	[
		'#000', '#030', '#060', '#090', '#0c0', '#0f0',
		'#003', '#033', '#063', '#093', '#0c3', '#0f3',
		'#006', '#036', '#066', '#096', '#0c6', '#0f6',
		'#009', '#039', '#069', '#099', '#0c9', '#0f9',
		'#00c', '#03c', '#06c', '#09c', '#0cc', '#0fc',
		'#00f', '#03f', '#06f', '#09f', '#0cf', '#0ff',
		'#f00', '#f30', '#f60', '#f90', '#fc0', '#ff0',
		'#f03', '#f33', '#f63', '#f93', '#fc3', '#ff3',
		'#f06', '#f36', '#f66', '#f96', '#fc6', '#ff6',
		'#f09', '#f39', '#f69', '#f99', '#fc9', '#ff9',
		'#f0c', '#f3c', '#f6c', '#f9c', '#fcc', '#ffc',
		'#f0f', '#f3f', '#f6f', '#f9f', '#fcf', '#fff',
	],

	//	[CH]	初始化
	init		:	function ( x )
	{
		if( is_undefined( this.dhtml ) || !is_object( this.dhtml ) )
			this.dhtml	=	new dhtml();

		if( is_undefined( this.widget ) || !is_object( this.widget ) )
			this.widget	=	new	widget();

		var	$M		=	this;

		/**
		 *	这里修改一个值
		 *	如果x的值为2的话则创建一个空对象
		 *	用来响应上传文件后的响应处理
		 *	Fix	:	2010-4-6
		 */
		//if( x == '2' )
		//	return;
		$M.loadSaveData();

		$M.editorMode	=	parseInt( x );
		$M.getEditor();
		$M.setTools();
		$M.getEffect();
		//$M.tempSave();
	},

	//	[CH]	读取临时存档
	loadSaveData	:	function ()
	{
		if( MDK.Cookie.isset( 'tempData' ) && MDK.Cookie.isset( 'sid' )/* && MDK.Cookie.get( 'sid' ) == currentFid*/ )
			if( confirm( '你有一篇未发布的草稿，是否要继续编辑该文章？' ) )
			{
				$( this.editPre + 'source' ).value	=	MDK.Cookie.get( 'tempData' );
				currentFid	=	MDK.Cookie.isset( 'sid' );
			}
		//alert( currentFid );
	},

	//	[CH]	临时保存文章
	tempSave	:	function ()
	{
		/**
		 *	由于COOKIE的大小限制
		 *	放弃了由COOKIE保存的方案
		 *	Fix	:	2010-4-7
		 */
		var	postsValue	=	this.editorMode == '1' ? this.editor.body.innerHTML : $( $M.editPre + 'source' ).value;
		var	titleValue	=	MDK.String.strip_tags( MDK.String.trim( $( 'meEditor-plus-title' ).value ) );
		var	tagsValue	=	MDK.String.strip_tags( MDK.String.trim( $( 'meEditor-plus-keyword' ).value ) );
		var	fid			=	currentFid;
		var	mid			=	$( 'meEditor-posts-mood' ).src;
			mid			=	mid.substr( mid.lastIndexOf( '/' ) + 1 ).split( '.' )[0];
			mid			=	parseInt( mid );

		//alert( mid );

		postsValue	=	this.widget.uh( postsValue );
		titleValue	=	this.widget.uh( titleValue );
		//alert( this.widget.hash( 84 ) );

		//if( postsValue.length <= 10 )
		//	return	alert( '当前文章长度太短' );
		//var date=new Date();
		//var	expireDays=10;
		//date.setTime( date.getTime()+expireDays*24*3600*1000 );
		//alert( cookiepath );

		//alert( postsValue );

		//MDK.Cookie.set( 'tempData', postsValue, 7, cookiepath );
		//alert( postsValue );
		//MDK.Cookie.set( 'sid', currentFid, 7, cookiepath );

		//document.cookie	=	'tempData=' + escape( postsValue ) + '; expire=' + date.toGMTString();
		//if( MDK.Cookie.isset( 'tempData' ) )
		//	alert( '草稿已经保存\r\n在您不清理Cookie的情况下\r\n系统将会在一周之内对您进行提醒' );
		//alert( MDK.Cookie.get( 'tempData' ) );
		//alert( postsValue );
		//$( 'save' ).onclick	=	function (e) { alert( postsValue ) };
	},

	doSubmit	:	function ()
	{
		var	title	=	MDK.String.trim( $( 'meEditor-plus-title' ).value );
		var	tags	=	MDK.String.trim( $( 'meEditor-plus-keyword' ).value );
		var	content	=	this.editorMode == '0' ? MDK.String.trim( $( 'meEditor_source' ).value ) : MDK.String.trim( this.editor.body.innerHTML );
		var	mood	=	$( 'meEditor-posts-mood' ).src;
		var	$M		=	this;

		if( title == '' || title == null )
			return	alert( '标题不能为空' );
		if( content == '' || content == null )
			return	alert( '文章内容不能为空' );

		var	content	=	this.widget.htmlentities( this.widget.clearFromWord( this.widget.uh( content ) ) );

		//return	alert( content );

		//	[CH]	这里需要加入Hash值验证
		var	url		=	'f=' + parseInt( currentFid ) + '&title=' + title + '&tags=' + tags + '&content=' + content + '&mood=' + mood + '&hash=' + this.widget.hash( 96 );

		var	writeHandle	=	function ()
		{
			if( MDK.Ajax.xmlHttp.readyState == 4 && MDK.Ajax.xmlHttp.status == 200 )
				{
					var	code	=	MDK.Ajax.xmlHttp.responseText.substr( 0, 6 );	//	[CH]	获取返回信息码
					var	msg		=	MDK.Ajax.xmlHttp.responseText.substr( 7 );		//	[CH]	获取返回信息
					var	sec		=	5;

					var	times	=	function ()
					{
						sec--;
						if( sec != 0 )
							var	timerId	=	window.setInterval( times, 1000 );
						else
						{
							clearInterval( timerId );
							location.reload( true );
						}
					}

					if( code == 'Err[4]' )
					{
						msg	=	'<a onclick="location.reload(true)">' + msg + '</a>';
						$M.dhtml.loading( msg );
						times();
					}
					else
					{
						msg	=	'<a onclick="$(\'MDKloading\').style.display=\'none\';">' + msg + '</a>';
						$M.dhtml.loading( msg );
					}
				}
				else
				{
					//	[CH]	禁止所有的编辑修改动作
					$( 'submit' ).unselectable	=	'on';
					$( 'save' ).unselectable	=	'on';
					/*$( 'login-authcode' ).unselectable	=	'on';
					$( 'do-login' ).unselectable		=	'on';*/

					//	[CH]	将窗口隐藏掉
					//$( 'me-login-window' ).style.display	=	'none';

					$M.dhtml.loading( '正在发布文章，请稍等...' );
				}
		}

		MDK.Ajax.init( 'post', './u/posts.php', writeHandle, url );
		//alert( content );
	},

	//	[CH]	设置各类效果
	getEffect	:	function ()
	{
		var	$M	=	this;
		var	$E	=	this.dhtml;

		var	title	=	$( 'meEditor-plus-title' );
		var	tags	=	$( 'meEditor-plus-keyword' );
		var	more	=	$( 'meEditor-plus-more' );
		var	advance	=	$( 'meEditor-plus-advance' );
		var	mood	=	$$( 'img', $( 'meEditor-plus-mood' ) );
		var	addfile	=	$( 'meEditor_sys_addfile' );
		var	save	=	$( 'save' );
		var submit	=	$( 'submit' );

		submit.onclick	=	function (e) { return $M.doSubmit() };
		save.onclick	=	function (e) { return $M.tempSave() };

		title.onclick	=	function (e)
		{
			$( 'meEditor-posts-title' ).className	=	'title-tags-left selected'
			$( 'meEditor-posts-tags' ).className = 'title-tags-left';
			return;
		};

		tags.onclick	=	function (e)
		{
			$( 'meEditor-posts-title' ).className	=	'title-tags-left'
			$( 'meEditor-posts-tags' ).className = 'title-tags-left selected';
			return;
		};

		more.onclick	=	function (e) { return advance.style.display	=	more.checked == true ? 'block' : 'none'; }

		addfile.onclick	=	function (e) { return $M.newUpload() };

		//$( 'me-meEPlus-title' ).onmousedown	=	function (e) { return $E.drag( this, e, $( 'me-meEPlus-window' ) ); };
		$( 'ubb-mode' ).onclick	=	function (e)
		{
			$M.editorMode	=	0;
			$M.getEditor();
		}
		$( 'wysiwyg-mode' ).onclick	=	function (e)
		{
			$M.editorMode	=	1;
			$M.getEditor();
		}

		for( var i = 0, j = mood.length; i < j; i++ )
		{
			mood[i].onclick	=	function (e) { return $( 'meEditor-posts-mood' ).src = this.src };
		}
	},

	//	[CH]	创建新的上传动作
	newUpload	:	function ()
	{
		var	$M	=	this;
		var	$E	=	this.dhtml;

		var	inputs	=	$$( 'input', $( 'uploadFiles' ) );
		$M.attachTotal	=	is_undefined( $M.attachTotal ) ? 0 : $M.attachTotal;

		//	[CH]	验证文件类型
		var	testFilesExt	=	function ( value )
		{
			return	1;
		}

		var	uploadFiles	=	function ( obj )
		{
			//alert( obj.value );
			var	value	=	MDK.String.trim( obj.value );
			//var	uploadlist	=	$( 'uploadlist' );
			var	id			=	parseInt( obj.id.substr( 11 ) );

			if( !testFilesExt( value ) )
			{
				delFiles( id );
				//$M.delAttachFile( id );
				return alert( 'Err : 上传文件类型有误' );
			}

			//alert( id );
			//obj.disabled	=	true;
			$M.attachTotal	=	parseInt( obj.id.substr( 11 ) );
			//uploadlist.style.display	=	'';

			//var	$D	=	$E.create( 'div', 'fileInfo_' + id, $( 'fileList' ), { display : 'none' }, '<a id="delFiles_' + id + '">删除</a> <a id="insertFiles_'+ id +'">插入到文档</a> ' + value );

			//var	$D	=	$E.create( 'div', 'fileInfo_' + id, $( 'fileList' ), 'fileInfo', '<a id="delFiles_' + id + '">删除</a> <a id="uploadFiles_'+ id +'">上传</a> ' + value );
			//var	$D	=	$E.create( 'div', 'fileInfo_' + id, $( 'fileList' ), 'fileInfo', '文件上传中... ' + value );

			//var	as	=	$$( 'a', $( 'fileList' ) );
			//for( var i = 0, j = as.length; i < j; i++ )
			//{
			//	if( as[i].id.indexOf( 'delFiles_' ) != -1 )
			//		as[i].onclick	=	function (e) { return delFiles( parseInt( this.id.substr( 9 ) ) ) };

				/*if( as[i].id.indexOf( 'insertFiles_' ) != -1 )
				{
					as[i].unselectable	=	'on';
					as[i].onclick	=	function (e) { return insertFiles( parseInt( this.id.substr( 12 ) ) ) };
				}*/
				/*if( as[i].id.indexOf( 'uploadFiles_' ) != -1 )
				{
					as[i].unselectable	=	'on';
					as[i].onclick	=	function (e) { return toUploadFiles( this.id ) };
				}*/
			//}

			toUploadFiles( obj.id.substr( 11 ) );
		}

		var	toUploadFiles	=	function ( id )
		{
			var	obj	=	$( 'new_inputs_' + id );	//	[CH]	INPUT ID
			$( 'aUpload_' + id ).submit();
			obj.disabled	=	true;
			$( 'upload_status_' + id ).innerHTML	=	'文件上传中...';
			//$( id ).innerHTML	=	'文件上传中...';
		}

		/*var	insertFiles	=	function ( id )
		{
			return	$M.getEdit( false, false, '[local]' + id + '[/local]' );
		}*/

		var	delFiles	=	function ( id )
		{
			//alert( id )
			var	inputs	=	$( 'container_' + id );
			//try{ var fileInfo = $( 'fileInfo_' + id ) } catch (e) {};
			//var	regExp		=	new RegExp( '\\[local\\]' + id + '\\[\\/local\\]', 'ig' );

			//$M.editor.body.innerHTML	=	$M.editor.body.innerHTML.replace( regExp, '' );

			try{ $E.remove( inputs, $( 'uploadFiles' ) ) } catch (e) {};
			//try{ $E.remove( fileInfo, $( 'fileList' ) ) } catch (e) {};

			//var	oDiv	=	$$( 'div', $( 'fileList' ) );
			//if( oDiv.length <= 0 )
			//	$( 'uploadlist' ).style.display	=	'none';
		}

		var	newFileInput	=	function ( id )
		{
			var	newForms	=	'<form id="aUpload_' + id + '" action="./kernel/upload.inc.php" target="uploadIframe" method="post" enctype="multipart/form-data"></form>';
			var	oDiv	=	$E.create( 'div', 'container_' + id, $( 'uploadFiles' ), { margin : '0', padding : '0', clear : 'both' }, newForms );

			//$( 'uploadFiles' ).insertBefore( newForms );
			//var	$D	=	$E.create( 'input', { id : 'new_inputs_' + id, type : 'file', name : 'upload'/*, unselectable : 'on'*/ }, /*$( 'uploadFiles' )*/$( 'aUpload_' + id ),  { display : 'block', width : '400px' } );

			$( 'aUpload_' + id ).innerHTML	=	'<input id="new_inputs_' + id + '" type="file" name="upload" style="width:400px;float:left" /><input name="uploadKeys" type="hidden" value="' + id + '" /><div id="upload_status_' + id + '" style="float:left;margin-left:5px;padding-top:3px"></div>';
			$( 'new_inputs_' + id ).onchange	=	function (e) { return uploadFiles( this ) };
		}

		if( inputs.length <= 0 )
		{
			newFileInput( $M.attachTotal + 1 )
		}
		else
		{
			for( var i = 0, j = inputs.length; i < j; i++ )
			{
				if( inputs[i].value == '' || inputs[i].value == null )
				{
					return	alert( '你还有一个未进行上传的文件待处理' );
					//break;
				}
			}

			newFileInput( $M.attachTotal + 1 );
		}
	},

	getAttachFileName	:	function ( obj )
	{
		try{ var	id	=	obj.alt.substr( 0, 32 ) }
		catch(e)
		{
			var	id	=	MDK.String.strip_tags( obj.parentNode.innerHTML ).split( ' ' )[3].substr( 0, 32 );
			//alert( id );
		};
		return	id;
	},

	insertToDoc	:	function ( obj )
	{
		//alert( obj.parentNode.innerHTML );
		//	[CH]	FF居然无法获取对象的alt值
		/*try{ var	id	=	obj.alt.substr( 0, 32 ) }
		catch(e)
		{
			var	id	=	MDK.String.strip_tags( obj.parentNode.innerHTML ).split( ' ' )[2].substr( 0, 32 );
			//alert( id );
		};*/
		var	fileName	=	this.getAttachFileName( obj );
		obj.unselectable	=	'on';
		obj.title	=	'该附件已插入到文档';
		return	this.getEdit( 0, 0, '[local]' + fileName + '[/local]' );
	},

	delAttachFile	:	function ( obj, method )
	{
		//alert( id )
		obj.unselectable	=	'on';
		var	id	=	is_number( obj ) ? parseInt( obj ) : obj.id.substr( 9 );
		var	$E	=	this.dhtml;
		var	inputs	=	$( 'container_' + id );
		//try{ var fileInfo = $( 'fileInfo_' + id ) } catch (e) {};
		var	fileName	=	this.getAttachFileName( obj );
		var	regExp		=	new RegExp( '\\[local\\]' + fileName + '\\[\\/local\\]', 'ig' );

		if( method )
			this.editor.body.innerHTML	=	this.editor.body.innerHTML.replace( regExp, '' );

		try{ $E.remove( inputs, $( 'uploadFiles' ) ) } catch (e) {};
		//try{ $E.remove( fileInfo, $( 'fileList' ) ) } catch (e) {};
		//var	oDiv	=	$$( 'div', $( 'fileList' ) );
		//if( oDiv.length <= 0 )
		//	$( 'uploadlist' ).style.display	=	'none';
	},

	//	[CH]	设置工具架
	setTools	:	function ()
	{
		var	$M		=	this;
		var	$E		=	this.dhtml;

		for( var i = 0, j = $M.tools.length; i < j; i++ )
		{
			if( $M.tools[i] != 'split' )
			{
				var	$D	=	$E.create( 'img', { id : 'meEditor_' + $M.tools[i], src : $M.imgDir + $M.tools[i] + '.gif', unselectable : 'on' }, $( 'meEditor-plus-buttons' ), { border : '1px solid #fff', marginRight : '1px' } );
				$D.onmouseover	=	function (e) { return $E.setStyle( this, { border : '1px solid #cce0f5' } ) };
				$D.onmouseout	=	function (e) { return $E.setStyle( this, { border : '1px solid #fff' } ) };
			}
			else
				var	$D	=	$E.create( 'img', { id : $M.tools[i], src : $M.imgDir + $M.tools[i] + '.gif' }, $( 'meEditor-plus-buttons' ), { margin : '0 5px 0 5px' } );

			//	[CH]	IE下一个某明奇妙的BUG
			//	[CH]	个别图片不能按照图片高度显示
			//	[CH]	Fix : 2009-11-5
			if( MDK.Browser.name == 'IE' )
				$D.height	=	'21';
			$D.onclick	=	function (e) { return $M.listenUserAction( this.id.substr( 9 ) ) };
		}

		for( var i = 0; i < 15; i++ )
		{
			var	$D	=	$E.create( 'img', { id : 'meEditor_smilie_' + i, src : $M.smilieDir + i + '.gif', unselectable : 'on' }, $( 'meEditor-plus-smiles' ), { margin : '0 auto', marginTop : '10px' } );
			$D.onclick	=	function (e) { return $M.listenUserAction( this.id.substr( 9, 6 ), '<img src="' + this.src + '" unselectable="on" />' ) };
		}

		var	fontname	=	$E.create( 'div', { id : 'meEditor_fontname', unselectable : 'on' }, $( 'meEditor-plus-buttons' ), { position : 'absolute', left : '530px', top : '3px', width : '102px', height : '16px', border : '1px solid #fff', padding : '3px 0 2px 4px', cursor : 'pointer' }, '字体' );
		var	fontsize	=	$E.create( 'div', { id : 'meEditor_fontsize', unselectable : 'on' }, $( 'meEditor-plus-buttons' ), { position : 'absolute', left : '642px', top : '3px', width : '34px', height : '16px', border : '1px solid #fff', padding : '3px 0 2px 4px', cursor : 'pointer' }, '大小' );

		fontname.onclick	=	fontsize.onclick	=	function (e) { return $M.listenUserAction( this.id.substr( 9 ) ) };
		fontname.onmouseover	=	fontsize.onmouseover	=	function (e) { return $E.setStyle( this, { border : '1px solid #cce0f5' } ) };
		fontname.onmouseout	=	fontsize.onmouseout	=	function (e) { return $E.setStyle( this, { border : '1px solid #fff' } ) };
	},

	//	[CH]	监听用户动作
	listenUserAction	:	function ( cmd, value )
	{
		if( cmd == 'split' )
			return;

		var	$M	=	this;
		switch ( cmd )
		{
			case	'url'	:
			case	'email'	:
			case	'quote'	:
			case	'hide'	:
			case	'flv'	:
			case	'image'	:
				alert( '研发中' );
			break;
			case	'code'	:
				$( 'me-meEditor-popup-' + cmd ).style.display = 'block';
				$M.getPopup( cmd );
			break;

			case	'smilie'	:
				$M.getEdit( false, false, value );
			break;

			case	'fontname'	:
			case	'fontsize'	:
			case	'forecolor'		:
			case	'backcolor'		:
				$M.getFont( cmd );
			break;

			default	:
				$M.getEdit( cmd );
			break;
		}
	},

	//	[CH]	开启浮动层
	getPopup	:	function ( cmd )
	{
		var	$M	=	this;
		switch ( cmd )
		{
			case	'code' :
				$( 'sub-add-source-code' ).onclick = function (e)
					{
						if( $( 'source-code-type' ).value == '0' )
						{
							return	alert( '请选择代码类型' );
						}

						if( MDK.String.trim( $( 'add-source-code' ).value ) == '' )
						{
							return	alert( '请输入代码' );
						}

						//	[CH]	有BUG存在
						//	[CH]	等待使用UBB模式修正
						var	value	=	'[code type=' + $M.codeType[$( 'source-code-type' ).value] + ']<pre>' + $( 'add-source-code' ).value + '</pre>[/code]';
						$M.getEdit( 0, 0, value );
						$( 'source-code-type' ).value	=	0;
						$( 'add-source-code' ).value = '';
						$( 'me-meEditor-popup-code' ).style.display = 'none';
						return	value	=	null;
					};
				$( 'res-add-source-code' ).onclick = function (e)
					{
						$( 'add-source-code' ).value = '';
						return $( 'me-meEditor-popup-code' ).style.display = 'none';
					};
			break;
		}
	},

	//	[CH]	关闭浮动层
	closePopupDiv	:	function ( id )
	{
		var	$M	=	this;

		var	popup	=	$$( 'div', $( 'me-meEditor-plus' ) );

		for( var i = 0, j = popup.length; i < j; i++ )
		{
			if( id && popup[i].id == id )
				continue;

			if( popup[i].id.indexOf( '_popup_div' ) != -1 )
				popup[i].style.display	=	'none';
		}
	},

	//	[CH]	设置字体
	getFont		:	function ( cmd )
	{
		var	$M	=	this;
		var	$E	=	this.dhtml;
		var	id	=	$M.editPre + cmd + '_popup_div';

		//	[CH]	设置浮动层的定位
		switch ( cmd )
		{
			case	'fontname'	:
				var	arr		=	$M.family;
				var	left	=	'577px';
			break;

			case	'fontsize'	:
				var	arr		=	$M.size;
				var	left	=	'689px';
			break;

			case	'forecolor'	:
				var	arr		=	$M.color;
				var	left	=	'528px';
			break;

			case	'backcolor'	:
				var	arr		=	$M.color;
				var	left	=	'551px';
			break;
		}

		//	[CH]	如果菜单已经开启,则关闭掉
		if( $( id ) )
		{
			$( id ).style.display = $( id ).style.display == 'block' ? 'none' : 'block';
		}
		else
		{
			var	top	=	MDK.Browser.name == 'IE' ? '307px' : '309px';
			var	$D	=	$E.create( 'div', id, $( 'me-meEditor-plus' ), { position : 'absolute', top : top, left : left } );
			var	ul	=	$E.create( 'ul', false, $D );

			$E.getFront( $D );

			for( var x in arr )
			{
				switch ( cmd )
				{
					case	'fontname'	:
						var	li	=	$E.create( 'li', false, ul, { cursor : 'pointer', fontFamily : arr[x], fontSize : '12px' }, arr[x] );
					break;

					case	'fontsize'	:
						var	li	=	$E.create( 'li', false, ul, { cursor : 'pointer' }, '<font size=' + arr[x] + '>' + arr[x] + '</font>' );
					break;

					case	'forecolor'	:
					case	'backcolor'	:
						var	li	=	$E.create( 'li', false, ul, { cursor : 'pointer', backgroundColor : arr[x] } );
					break;
				}
			}
		}

		$M.closePopupDiv( id );

		var	lis	=	cmd == 'fontsize' ? $$( 'font', $D ) : $$( 'li', $D );
		for( var i = 0, j = lis.length; i < j; i++ )
		{
			lis[i].unselectable	=	'on';
			lis[i].onclick	=	function (e) { return $M.getEdit( cmd, $M.rgb2web( cmd, this ) ) };
		}
	},

	//	[CH]	转换颜色
	rgb2web		:	function ( menu, obj )
	{
		var	$M	=	this;

		$( $M.editPre + menu + '_popup_div' ).style.display = 'none';

		if( menu != 'forecolor' && menu != 'backcolor' )
			return	obj.innerHTML;

		var	color	=	obj.style.backgroundColor;

		function	addbit ( value, length )
		{
			return	value	=	value.length < length ? value + value : value;
		}


		if ( MDK.Browser.name == 'IE' )
		{
			var	r	=	color.substr( 1, 1 );
			var	g	=	color.substr( 2, 1 );
			var	b	=	color.substr( 3, 1 );
		}
		else
		{
			var	r	=	parseInt( color.replace( /rgb\(([0-9]+?),\s+?([0-9]+?),\s+?([0-9]+?)\)/ig, '$1' ) );
			var	g	=	parseInt( color.replace( /rgb\(([0-9]+?),\s+?([0-9]+?),\s+?([0-9]+?)\)/ig, '$2' ) );
			var	b	=	parseInt( color.replace( /rgb\(([0-9]+?),\s+?([0-9]+?),\s+?([0-9]+?)\)/ig, '$3' ) );
		}
		return	'#' + addbit( r.toString( 16 ), 2 ) + addbit( g.toString( 16 ), 2 ) + addbit( b.toString( 16 ), 2 );
	},

	//	[CH]	执行编辑动作
	getEdit		:	function ( cmd, format, value )
	{
		var	$M	=	this;
		if( $M.editorMode == 0 )
			return alert( 'Err : HTML代码模式下该功能无效' );

		this.doc.focus();
		if( !value && cmd )
		{
			try
			{
				this.editor.execCommand( cmd, false, format );
			}
			catch( e )
			{
				alert( "错误:" + e + "\r\n可能的原因是:浏览器不支持该功能" );
			}
		}
		else
		{
			try
			{
				if( MDK.Browser.name == 'IE' )
				{
					var sel	=	this.editor.selection.createRange();
					//alert( value );
					sel.pasteHTML( value );
					sel.collapse( false );
					sel.select();
				}
				else
				{
					var sel	=	this.doc.getSelection();
					this.editor.execCommand( 'insertHtml', false, value );
				}
			}
			catch( e )
			{
				alert( "错误:" + e + "\r\n可能的原因是:浏览器不支持该功能" );
			}
		}
		this.doc.focus();
	},

	//	[CH]	创建编辑器
	getEditor	:	 function ()
	{
		var $M		=	this;
		var	$E		=	this.dhtml;
		var meEditorObject	=	$( $M.editPre + 'source' );

		$M.editorMode = !is_undefined( $M.editorMode ) ? $M.editorMode : 1;

		if( $M.editorMode == 1 )
		{
			meEditorObject.style.display	=	'none';

			if( $( $M.editPre + 'iframe' ) )
			{
				$M.editor	=	$( $M.editPre + 'iframe' ).contentWindow.document;
				$( $M.editPre + 'iframe' ).style.display	=	'block';
			}
			else
			{
				$M.editor	=	$E.create( 'iframe', { id : $M.editPre + 'iframe', frameBorder : 0 }, meEditorObject.parentNode );
				$M.editor	=	$M.editor.contentWindow.document;
				$M.editor.designMode	=	'on';
			}

			$M.doc	=	$( $M.editPre + 'iframe' ).contentWindow;
			$M.editor.open( 'text/html', 'replace' );
			$M.editor.write( meEditorObject.value );
			$M.editor.close();

			//$M.editor.onclick	=	function (e) { return $M.closePopupDiv() };
			//$M.editor.onkeyup	=	function (e) { return $M.uh( $M.editor.body.innerHTML ) };
			//	[CH]	兼容FF
			addEventHandle( $M.editor, 'click', function (e) { return $M.closePopupDiv() } );

			//if( MDK.Browser.name == 'IE' )
			try{ $M.editor.createStyleSheet( $M.iframeCssFiles ); }
			catch(e)
			{
				var	oCss	=	$M.editor.createElement( 'style' );
				oCss.type	=	'text/css';
				oCss.innerHTML	=	'@import url(' + $M.iframeCssFiles + ')';
				$M.editor.getElementsByTagName( 'head' )[0].appendChild( oCss );
			}
			$E.setStyle( $( 'ubb-mode' ), { borderBottom : '1px solid #cce0f5' } );
			$E.setStyle( $( 'wysiwyg-mode' ), { borderBottom : '1px solid #fff' } );
		}
		else
		{
			if( $( $M.editPre + 'iframe' ) )
			{
				$( $M.editPre + 'iframe' ).style.display = 'none';
				meEditorObject.value	=	$M.editor.body.innerHTML;
			}
			meEditorObject.style.display	=	'block';
			$E.setStyle( $( 'ubb-mode' ), { borderBottom : '1px solid #fff' } );
			$E.setStyle( $( 'wysiwyg-mode' ), { borderBottom : '1px solid #cce0f5' } );
		}
	}
})